package com.lihan.controller;

import com.lihan.common.Result;
import com.lihan.enums.OperatorFriendRequestTypeEnum;
import com.lihan.enums.SearchFriendsStatusEnum;
import com.lihan.model.entity.FriendsRequest;
import com.lihan.model.entity.User;
import com.lihan.model.vo.FriendsRequestVo;
import com.lihan.model.vo.MyFriendsVo;
import com.lihan.model.vo.UserVo;
import com.lihan.services.IFriendService;
import com.lihan.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: FriendController
 * Package: com.lihan.controller
 * Description:
 *
 * @Author 李涵
 * @Create 2025-05-08 17:18
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/friend")
@Api(tags = "好友管理模块")
public class FriendController {

    @Resource
    private IFriendService friendService;

    @Resource
    private IUserService userService;


    @GetMapping("/searchFriend")
    @ApiOperation(value = "搜索好友")
    public Result searchFriend(String myUserId, String friendUserName){
        // 1.判断搜索的用户是否存在，如果不存在，返回无此用户，如果是自己，返回不能是自己，如果搜索的已经是好友，返回该用户已经是你的好友
        Integer status = friendService.preconditionSearchFriend(myUserId,friendUserName);

        if(status == SearchFriendsStatusEnum.SUCCESS.status){
            //2. 通过用户名查询好友
            User user = userService.getUserByUsername(friendUserName);
            //3. 将返回信息拷贝到没有密码等的封装UserVo返回给前端
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            return Result.ok(userVo);
        }else {
            return Result.errorMsg(SearchFriendsStatusEnum.getMsgByKey(status)); //返回对应的错误信息
        }

    }

    @GetMapping("/addFriendRequest")
    @ApiOperation("添加好友")
    public Result addFriendRequest(String myUserId,String friendUserName){
        //判空
        if(StringUtils.isBlank(myUserId) || StringUtils.isBlank(friendUserName)) {
            return Result.errorMsg("好友信息为空");
        }
        friendService.sendFriendRequest(myUserId,friendUserName);
        return Result.ok();
    }

    @GetMapping("/queryFriendRequst")
    @ApiOperation("查询添加好友请求列表")
    public Result queryFriendRequst(String userId){
        //判空
        List<FriendsRequestVo> friendsRequestList = friendService.queryFriendRequestList(userId);
        return Result.ok(friendsRequestList);
    }

    @GetMapping("/operFriendRequest")
    @ApiOperation("处理好友请求")
    public Result operFriendRequest(String acceptUserId,String sendUserId,Integer operType){
        //构造成好友请求的对象
        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setAcceptUserId(acceptUserId);
        friendsRequest.setSendUserId(sendUserId);

        if(operType == OperatorFriendRequestTypeEnum.IGNORE.type){
            //满足此条件将需要对好友请求表中的数据进行删除操作
            friendService.deleteFriendRequest(friendsRequest);
        }else if(operType == OperatorFriendRequestTypeEnum.PASS.type){
            //满足此条件向好友表中添加一条记录，同时删除好友请求表中的记录
            friendService.passFriendRequest(sendUserId,acceptUserId);
            friendService.deleteFriendRequest(friendsRequest);
        }
        //查询好友表中的列表数据
        List<MyFriendsVo> myFriends = friendService.queryMyFriends(acceptUserId);
        return Result.ok(myFriends);
    }

    @GetMapping("/myFriends")
    @ApiOperation("查询好友列表")
    public Result myFriends(String userId){
        if(StringUtils.isBlank(userId)){
            return Result.errorMsg("用户id为空");
        }
        List<MyFriendsVo> myFriends = friendService.queryMyFriends(userId);
        return Result.ok(myFriends);
    }
}
